package com.koudai.finance.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Description: 日期工具类
 * @Author ls
 * @Date 2021/07/20 16:42
 */
@Slf4j
public class DateUtil {

    /**
     * 标准格式化时间
     */
    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 日期格式化时间
     */
    public static final String DATETIME_FORMAT_DAY = "yyyy-MM-dd";

    /**
     * @Description: 获取指定月份偏移量的开始时间
     * @Author ls
     * @Date 2021/06/16 19:23
     */
    public static String getOffsetMonthStart(int offset, String format) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime firstDayOfLastMonth = now.plusMonths(offset).withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
        return firstDayOfLastMonth.format(dtf);
    }

    /**
     * @Description: 获取指定天数偏移量的凌晨时间
     * @Author: ls
     * @Date: 2020/09/25 15:29
     */
    public static String getOffsetDayStart(int offset, String format) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime firstDayOfLastMonth = now.plusDays(offset).withHour(0).withMinute(0).withSecond(0);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
        return firstDayOfLastMonth.format(dtf);
    }

    /**
     * @Description: 获取指定天数偏移量
     * @Author: ls
     * @Date: 2020/09/25 15:29
     */
    public static String getOffsetDay(int offset, String format) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime firstDayOfLastMonth = now.plusDays(offset);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
        return firstDayOfLastMonth.format(dtf);
    }

    /**
     * @Description: 获取两个日期的小时差
     * @Author ls
     * @Date 2021/07/16 15:14
     */
    public static long diffHour(Date beginDate, Date endDate) {
        long nh = 1000 * 60 * 60;
        long diff = endDate.getTime() - beginDate.getTime();
        return diff / nh;
    }

    /**
     * @Description: 获取两个日期的时间差
     * @Author ls
     * @Date 2021/07/16 15:14
     */
    public static long diffNm(String beginTime, String endTime) {
        long diff = 0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            //获取两个时间的毫秒时间差
            diff = format.parse(endTime).getTime() - format.parse(beginTime).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return diff;
    }

    /**
     * @Description: 根据指定的模板获取当前时间
     * @author: ls
     * @Date: 2020/7/16 16:51
     */
    public static String getCurrTimeStr(String format) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
        return now.format(dtf);
    }

    /**
     * @Description: 时间戳转时间
     * @author: ls
     * @Date: 2020/7/16 16:51
     */
    public static String stampToStr(Long milliseconds, String format) {
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(milliseconds / 1000, 0, ZoneOffset.ofHours(8));
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
        return localDateTime.format(dtf);
    }

    /**
     * @Description: 转换时间
     * @Author ls
     * @Date 2023/04/12 20:12
     */
    public static String transformStr(String createTime, String format) {
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = formatter1.parse(createTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat formatter2 = new SimpleDateFormat(format);
        return formatter2.format(date);
    }
}